/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spatialmarketplace.cem.action;

import java.util.ArrayList;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.util.Log;
import spatialmarketplace.cem.dao.implementation.ForumPostDaoImpl;
import spatialmarketplace.cem.model.ForumPost;

/**
 *  This bean controls the forum for both groups and resources.
 * @author Camila
 */
public class ForumActionBean extends BaseActionBean {
    
    private static final Log logger = Log.getInstance(ForumActionBean.class);
    private static final String FORM = "/WEB-INF/jsp/forum.jsp";
    private ArrayList<ForumPost> forumPosts;
    private static ForumPostDaoImpl forumPostDao = new ForumPostDaoImpl();
    protected ForumPost newPost = new ForumPost();
    protected Integer groupOrResourceId = Integer.valueOf(2);
    protected Integer userId = Integer.valueOf(2);
    protected boolean belongsToGroup;
    protected Integer updatePostId = 0;
    
    @DefaultHandler
    public Resolution form() {
        //These 2 properties have to be modified in the jsp before loading.
        groupOrResourceId = Integer.valueOf(getContext().getGroupOrResourceId());
        belongsToGroup = Boolean.valueOf(getContext().getForumBelongsToGroup());
        loadForum(groupOrResourceId, belongsToGroup);
        return new ForwardResolution(FORM);
    }

    /**
     * Call this method to load the initial information of the resource to display.
     * @param groupOrResourceId
     * @param belongsToGroup 
     */
    protected void loadForum(Integer groupOrResourceId, boolean belongsToGroup) {
        if (belongsToGroup) {
            forumPosts = getForumPostDao().readGroupForum(groupOrResourceId);
            System.out.println("ArrayList size after readGroupForum: " + forumPosts.size());
            
        } else {
            forumPosts = getForumPostDao().readResourceForum(groupOrResourceId);
        }
    }

    /**
     * Increase the likes count of a post and relate it with a user.
     * @return forum jsp
     */
    public Resolution likePost() {        
        Integer pId = Integer.valueOf(getContext().getRequest().getParameter("idPost"));
//        System.out.println("idPost: "+ pId);
        
        getForumPostDao().likePost(pId);
        
        return new RedirectResolution(ForumActionBean.class);
    }

    /**
     * Report a post. The id is received as a parameter from the jsp.
     * @return forum.jsp
     */
    public Resolution reportPost() {        
        Integer pId = Integer.valueOf(getContext().getRequest().getParameter("idPost"));
//        System.out.println("idPost: "+ pId);        
        getForumPostDao().reportPost(pId);
        return new RedirectResolution(ForumActionBean.class);
    }

    /**
     * Save a new post to the forum.
     * @return forum.jsp
     */
    public Resolution postComment() {
        //Implement the get and set of each post and the complete resourceForum to make these actions easier.
//        System.out.println ("ArrayList size after param: "+forumPosts);
        getForumPostDao().postToForum(newPost, getContext().getForumBelongsToGroup());
        return new RedirectResolution(ForumActionBean.class);
    }
    
    public static ForumPostDaoImpl getForumPostDao() {
        return forumPostDao;
    }
    
    public static void setForumPostDao(ForumPostDaoImpl forumPostDao) {
        ForumActionBean.forumPostDao = forumPostDao;
    }
    
    public boolean isBelongsToGroup() {
        return belongsToGroup;
    }
    
    public void setBelongsToGroup(boolean belongsToGroup) {
        this.belongsToGroup = belongsToGroup;
    }
    
    public ArrayList<ForumPost> getForumPosts() {
        return forumPosts;
    }
    
    public void setForumPosts(ArrayList<ForumPost> forumPosts) {
        this.forumPosts = forumPosts;
    }
    
    public Integer getGroupOrResourceId() {
        return groupOrResourceId;
    }
    
    public void setGroupOrResourceId(Integer groupOrResourceId) {
        this.groupOrResourceId = groupOrResourceId;
    }
    
    public ForumPost getNewPost() {
        return newPost;
    }
    
    public void setNewPost(ForumPost newPost) {
        this.newPost = newPost;
    }

    /**
     * Retrieve the relevant forum as a list of forum posts.
     * @param l list
     * @param id of the forum.
     * @return 
     */
    public static ForumPost searchForumPost(ArrayList<ForumPost> l, Integer id) {
//        System.out.println ("Searching for ForumPost: "+id);
        ForumPost p = new ForumPost();
        if (l == null) {
            return p;
        }
        
        for (int i = 0; i < l.size(); i++) {
            ForumPost tmp = l.get(i);
            if (tmp.getIdPost() == id) {
                return tmp;
            }
        }
        return p;
    }
    
    public Integer getUpdatePostId() {
        return updatePostId;
    }
    
    public void setUpdatePostId(Integer updatePostId) {
        this.updatePostId = updatePostId;
    }
    
    public Integer getUserId() {
        return userId;
    }
    
    public void setUserId(Integer userId) {
        this.userId = userId;
    }
}
